package com.bk.service.cardInfo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.bk.entity.CardInfo;
import com.bk.entity.UserInfo;
import com.bk.mapper.balance.BalanceMapper;
import com.bk.mapper.cardInfo.CardInfoMapper;
import com.bk.mapper.tranInfo.TranInfoMapper;
import com.bk.mapper.userInfo.UserInfoMapper;
import com.bk.util.GeneratorCardIdUtil;
import com.bk.util.Md5Util;
import com.bk.util.ReturnUtil;
import com.bk.util.TimeUtil;
import com.bk.vo.UserCardInfoVo;

@Service /** 参与卡信息的创建 */
public class CardInfoService {

	@Autowired
	private CardInfoMapper cardInfoMapper;

	@Autowired
	private UserInfoMapper userInfoMapper;
	
	@Autowired
	private TranInfoMapper tranInfoMapper;
	
	@Autowired
	private BalanceMapper balanceMapper;

	/** 第一次开户 成功返回1 uid身份证*/
	public ReturnUtil<CardInfo> firstCreateAccount(CardInfo cardInfo,UserInfo userInfo) {
		List<CardInfo> lc=new ArrayList<CardInfo>();
		UserInfo existUserInfo = userInfoMapper.isExistUserInfoByUid(userInfo.getUid());
		if(null==existUserInfo) {
			String cardId = GeneratorCardIdUtil.get_Bank_account();//自动生成18位银行卡号
			int userFlag=0;
			int cardInfoFlag=0;
			int balanceFlag=0;
			cardInfo.setCardPassword(Md5Util.getMd5(cardInfo.getCardPassword()));//密码加密
			cardInfo.setUserInfo(userInfo);//将用户信息传递进到卡信息
			cardInfo.setCardId(cardId);//设置银行卡号
			cardInfo.setCardLock(0);//默认未锁
			cardInfo.setTcreateTime(TimeUtil.getCurrentTimeUtil());//获取当前时间
			try {
				/** 创建用户信息 */
				userFlag=userInfoMapper.createUserInfo(cardInfo.getUserInfo());
				/** 创建卡号 */
				cardInfoFlag=cardInfoMapper.createAccountInfo(cardInfo, userInfo.getUid());
				/**给刚创建的卡赋默认金额值*/
				balanceFlag=cardInfoMapper.createOriginBalance(cardInfo.getCardId(),"0.0");
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(userFlag>0&&cardInfoFlag>0&&balanceFlag>0) {
				lc.add(cardInfo);
				return new ReturnUtil<CardInfo>(200,"账号创建成功",lc);
			}else {
				return new ReturnUtil<CardInfo>(400,"账号创建失败",null);
			}
		}else {
			return new ReturnUtil<CardInfo>(400,"当前用户已经开过户，请选择非第一次开户",null);
		}
	}
	
	/**验证该账户是否存在*/
	public ReturnUtil<CardInfo> nofirstCreateAccount(UserInfo userInfo, CardInfo cardInfo) {
		int cardInfoFlag=0;
		int balanceFlag=0;
		List<CardInfo> cardInfoList=new ArrayList<CardInfo>();
		UserInfo userFromDataBase=userInfoMapper.isExistUserInfo(userInfo.getUid(),userInfo.getUname());//判断用户是否存在
		if(null!=userFromDataBase) {
			CardInfo cardFromDataBase=cardInfoMapper.isExistCardInfo(userFromDataBase.getUid(),cardInfo.getCardId());//卡号是否存在
			if(null!=cardFromDataBase) {
				String cardId = GeneratorCardIdUtil.get_Bank_account();//自动生成18位银行卡号
				cardInfo.setCardLock(0);
				cardInfo.setCardId(cardId);
				cardInfo.setCardPassword(Md5Util.getMd5(cardInfo.getCardPassword()));//密码加密
				cardInfo.setTcreateTime(TimeUtil.getCurrentTimeUtil());
				cardInfo.setUserInfo(userInfo);
				cardInfoFlag=cardInfoMapper.createAccountInfo(cardInfo,userFromDataBase.getUid());//非第一次创建账户
				/**给刚创建的卡赋默认金额值*/
				balanceFlag=cardInfoMapper.createOriginBalance(cardInfo.getCardId(),"0.0");
				if(cardInfoFlag>0&&balanceFlag>0) {
					cardInfoList.add(cardInfo);
					return new ReturnUtil<CardInfo>(200,"账号创建成功",cardInfoList);
				}else {
					return new ReturnUtil<CardInfo>(400,"账号创建失败",null);
				}
			}else {
				return new ReturnUtil<CardInfo>(400,"该用户不曾有该卡号",null);
			}
		}else {
			return new ReturnUtil<CardInfo>(400,"该用户身份证信息有误",null);
		}
	}

	/**账户冻结或解冻*/
	public ReturnUtil<CardInfo> blockOrUnBlockAccount(int block, UserInfo userInfo, CardInfo cardInfo) {		
		UserCardInfoVo uc=cardInfoMapper.isExistCardByAccountIdAndUidAndName(cardInfo.getCardId(),userInfo.getUid(), userInfo.getUname(),userInfo.getUtelephone());
		if(uc.getCardInfoList().size()>0) {//是否有该账户 
			CardInfo cardInfoFromDataBase=uc.getCardInfoList().get(0);
			int blockFromDataBase=cardInfoFromDataBase.getCardLock();
			if(blockFromDataBase==1&&block==1) {
				return new ReturnUtil<CardInfo>(400,"该账户已处于冻结状态",null);
			}else if(blockFromDataBase==0&&block==0){
				return new ReturnUtil<CardInfo>(400,"该账户不处于冻结状态",null);
			}
			int returnCode = cardInfoMapper.blockOrUnBlockAccount(block,cardInfo.getCardId());
			if(returnCode>0&&block==1) {
				return new ReturnUtil<CardInfo>(200,"账户冻结成功",null);
			}else if(returnCode>0&&block==0) {
				return new ReturnUtil<CardInfo>(200,"账户解冻成功",null);
			}else {
				return new ReturnUtil<CardInfo>(400,"操作失败",null);
			}
		}else {
			return new ReturnUtil<CardInfo>(400,"请检查输入的账户信息",null);
		}
		
	}

	/**销户*/
	@Transactional
	public ReturnUtil<Integer> closeAccount(String cardId, String uname, String uid,String utelephone) {
		UserCardInfoVo uv = cardInfoMapper.isExistCardByAccountIdAndUidAndName(cardId, uid, uname, utelephone);
		if(null==uv) {
			return new ReturnUtil<Integer>(400,"当前用户不存在该账户，请检查销户信息",null);
		}else {
			try {
				//1.删除流水
				int deleteTranInfoCode=tranInfoMapper.deleteTranFlowByCardId(cardId);
				//2.删除账户金额
				int deleteBalanceCode=balanceMapper.deleteBalanceByCardId(cardId);
				//3.删除账户
				int deleteCardInfoCode=cardInfoMapper.deleteCardInfoByCardId(cardId,uid);
				if(0>deleteTranInfoCode||0>=deleteBalanceCode||0>deleteCardInfoCode) {
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//若其中出现问题手动事务回滚
				}else {
					return new ReturnUtil<Integer>(200,"操作成功",null);
				}
			} catch (Exception e) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}
		return new ReturnUtil<Integer>(400,"操作失败",null);
	}

}
